package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportApiApplyThirdpartyRpcService;
import com.hunttown.mes.rpc.domain.TransportApiApplyThirdpartyDTO;
import com.hunttown.mes.rpc.domain.query.TransportApiApplyThirdpartyDTOQuery;
import com.hunttown.mes.service.TransportApiApplyThirdpartyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportApiApplyThirdpartyConverts.convert;

/**
 * created by wangjunfu on 2022-04-10 09:48:00
 */
@Service
public class TransportApiApplyThirdpartyRpcServiceImpl implements TransportApiApplyThirdpartyRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportApiApplyThirdpartyRpcServiceImpl.class);

    private final TransportApiApplyThirdpartyService transportApiApplyThirdpartyService;

    @Autowired
    public TransportApiApplyThirdpartyRpcServiceImpl(TransportApiApplyThirdpartyService transportApiApplyThirdpartyService) {
        this.transportApiApplyThirdpartyService = transportApiApplyThirdpartyService;
    }

    @Override
    public TransportApiApplyThirdpartyDTO insert(TransportApiApplyThirdpartyDTO transportApiApplyThirdpartyDTO) {
        try {
            return convert(transportApiApplyThirdpartyService.insert(convert(transportApiApplyThirdpartyDTO)));
        } catch (Exception e) {
            logger.error("TransportApiApplyThirdparty create failed.", e);
            throw new RuntimeException("TransportApiApplyThirdparty create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportApiApplyThirdpartyDTO transportApiApplyThirdpartyDTO) {
        try {
            return transportApiApplyThirdpartyService.updateInfoById(convert(transportApiApplyThirdpartyDTO));
        } catch (Exception e) {
            logger.error("TransportApiApplyThirdparty update by id failed.", e);
            throw new RuntimeException("TransportApiApplyThirdparty update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportApiApplyThirdpartyDTOQuery transportApiApplyThirdpartyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportApiApplyThirdpartyDTOQuery.getW_id() != null && transportApiApplyThirdpartyDTOQuery.getW_id() > 0) {
                query.put("w_id", transportApiApplyThirdpartyDTOQuery.getW_id());
            }

            if (transportApiApplyThirdpartyDTOQuery.getIds() != null && !transportApiApplyThirdpartyDTOQuery.getIds().equals("")) {
                query.put("ids", transportApiApplyThirdpartyDTOQuery.getIds());
            }

            if (transportApiApplyThirdpartyDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportApiApplyThirdpartyDTOQuery.getStopFlag());
            }

            if (transportApiApplyThirdpartyDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportApiApplyThirdpartyDTOQuery.getDeleteFlag());
            }

            return transportApiApplyThirdpartyService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportApiApplyThirdparty update by query failed.", e);
            throw new RuntimeException("TransportApiApplyThirdparty update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportApiApplyThirdpartyDTO selectById(Integer id) {
        try {
            return convert(transportApiApplyThirdpartyService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportApiApplyThirdparty select by id failed.", e);
            throw new RuntimeException("TransportApiApplyThirdparty select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportApiApplyThirdpartyDTO selectWithoutEntId(Integer id) {
        try {
            return convert(transportApiApplyThirdpartyService.selectWithoutEntId(id));
        } catch (Exception e) {
            logger.error("TransportApiApplyThirdparty select by id failed.", e);
            throw new RuntimeException("TransportApiApplyThirdparty select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportApiApplyThirdpartyDTO selectObjByQuery(TransportApiApplyThirdpartyDTOQuery transportApiApplyThirdpartyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportApiApplyThirdpartyDTOQuery.getAppId())) {
                query.put("appId", transportApiApplyThirdpartyDTOQuery.getAppId());
            }

            if (transportApiApplyThirdpartyDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportApiApplyThirdpartyDTOQuery.getStopFlag());
            }

            if (transportApiApplyThirdpartyDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportApiApplyThirdpartyDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportApiApplyThirdpartyDTOQuery.getOrderBy())) {
                query.put("orderBy", transportApiApplyThirdpartyDTOQuery.getOrderBy());
            }

            return convert(transportApiApplyThirdpartyService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportApiApplyThirdparty select obj by query failed.", e);
            throw new RuntimeException("TransportApiApplyThirdparty select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportApiApplyThirdpartyDTO> getListForPage(TransportApiApplyThirdpartyDTOQuery transportApiApplyThirdpartyDTOQuery) {
        Query query = Query.page(transportApiApplyThirdpartyDTOQuery.getPn(), transportApiApplyThirdpartyDTOQuery.getPageSize());

        if (transportApiApplyThirdpartyDTOQuery.getId() != null && transportApiApplyThirdpartyDTOQuery.getId() > 0) {
            query.put("id", transportApiApplyThirdpartyDTOQuery.getId());
        }

        if (transportApiApplyThirdpartyDTOQuery.getIds() != null && !transportApiApplyThirdpartyDTOQuery.getIds().equals("")) {
            query.put("ids", transportApiApplyThirdpartyDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportApiApplyThirdpartyDTOQuery.getLikeName())) {
            query.put("likeName", transportApiApplyThirdpartyDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportApiApplyThirdpartyDTOQuery.getShowDate())) {
            query.put("showDate", transportApiApplyThirdpartyDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportApiApplyThirdpartyDTOQuery.getStartDate())) {
            query.put("startDate", transportApiApplyThirdpartyDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportApiApplyThirdpartyDTOQuery.getEndDate())) {
            query.put("endDate", transportApiApplyThirdpartyDTOQuery.getEndDate());
        }

        if (transportApiApplyThirdpartyDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportApiApplyThirdpartyDTOQuery.getStopFlag());
        }

        if (transportApiApplyThirdpartyDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportApiApplyThirdpartyDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(transportApiApplyThirdpartyDTOQuery.getOrderBy())) {
            query.put("orderBy", transportApiApplyThirdpartyDTOQuery.getOrderBy());
        }

        List<TransportApiApplyThirdpartyDTO> list = convert(transportApiApplyThirdpartyService.selectByQuery(query));
        Page<TransportApiApplyThirdpartyDTO> page = Page.from(transportApiApplyThirdpartyDTOQuery.getPn(), transportApiApplyThirdpartyDTOQuery.getPageSize(), list);
        page.setTotalItems(transportApiApplyThirdpartyService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportApiApplyThirdpartyService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportApiApplyThirdparty delete failed.", e);
            throw new RuntimeException("TransportApiApplyThirdparty delete failed, Please order your code.", e);
        }
    }
}